import { Message } from "@arco-design/web-vue";
import { createRouter, createWebHistory } from "vue-router";
import HomeView from "../views/HomeView.vue";

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: "/",
      redirect: "/admin",
    },
    {
      path: "/login",
      name: "login",
      component: () => import("@/views/login.vue"),
    },
    {
      path: "/about",
      name: "about",
      component: () => import("@/components/Layout.vue"),
    },
    {
      path: "/test",
      name: "test",
      component: () => import("@/views/home/Test.vue"),
    },
    {
      path: "/answerQues/:id",
      name: "answerQues",
      component: () => import("@/views/home/Answer.vue"),
    },
    {
      path: "/admin",
      name: "admin",
      component: () => import("@/components/Layout.vue"),
      children: [
        {
          path: "",
          name: "admin",
          component: () => import("../views/admin/Admin.vue"),
          meta: {
            auth: true,
          },
        },
        {
          path: "question",
          name: "question",
          component: () => import("../views/admin/Question.vue"),
          meta: {
            auth: true,
          },
        },
        {
          path: "student",
          name: "student",
          component: () => import("../views/admin/Student.vue"),
          meta: {
            auth: true,
          },
        },
        {
          path: "answer",
          name: "answer",
          component: () => import("../views/admin/Answer.vue"),
          meta: {
            auth: true,
          },
        },
      ],
    },
  ],
});

router.beforeEach((to, from, next) => {
  if (to.meta.auth) {
    if (window.localStorage.getItem("login")) {
      next();
    } else {
      Message.info("请先登录~");
      next("/login");
    }
  } else {
    next();
  }
});

export default router;
